﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Public;
using BUS;
namespace QuanLyKhoHangVatTu
{
    public partial class DonViTinh : DevExpress.XtraEditors.XtraForm
    {
        DonViTinh_PB dvt_pb = new DonViTinh_PB();
        DonViTinh_BUS dvt_bus = new DonViTinh_BUS();
        public DonViTinh()
        {
            InitializeComponent();
        }

        public DonViTinh(string MaDonViTinh, string DienGiai)
        {
            InitializeComponent();

            txtDonViTinh.Text = MaDonViTinh;
            txtMoTa.Text = DienGiai;
            txtDonViTinh.Properties.ReadOnly = true;
        }



        public DonViTinh_PB GetValues()
        {
            DonViTinh_PB.MaDonViTinh = txtDonViTinh.Text;
            DonViTinh_PB.DienGiai = txtMoTa.Text;
            return dvt_pb;
        }

        private bool CheckData()
        {
            if (txtDonViTinh.Text == string.Empty)
            {
                txtDonViTinh.ErrorText = "Đơn vị tính không được trống";
                return false;
            }
            
            if (dvt_bus.Exists(txtDonViTinh.Text))
            {
                if (Flag_PB.Sua == true)
                {
                    Flag_PB.Sua = false;
                    return true;

                }
                txtDonViTinh.ErrorText = "Mã trùng";
                return false;
            }

            return true;
        }
        private void btnLuu_Click(object sender, EventArgs e)
        {
            GetValues();
            if (CheckData())
            {
                    try
                    {
                        dvt_bus.ThemSuaDVT(dvt_pb);
                    }
                    catch(Exception ex)
                    {
                        if (ex.Message.Contains(" data would be truncated"))
                            XtraMessageBox.Show("Vượt quá ký tự cho phép", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    this.Close();
            }
            else
            {
                //MessageBox.Show("Đơn vị tính không được bỏ trống", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                return;
            }
        }

        private void btnLuu_Them_Click(object sender, EventArgs e)
        {

            txtDonViTinh.Properties.ReadOnly = false;
            GetValues();
            if (CheckData())
            {
                try 
                {

                    dvt_bus.ThemSuaDVT(dvt_pb);
                    txtDonViTinh.ResetText();
                    txtMoTa.ResetText();

                    // keu thang cha load
                    if (this.Tag != null)
                    {
                        IChildForm iChildForm = this.Tag as IChildForm;
                        if (iChildForm != null)
                            iChildForm.Nap();
                    }
                }
                catch (Exception ex)
                {
                    if (ex.Message.Contains(" data would be truncated"))
                        XtraMessageBox.Show("Vượt quá ký tự cho phép", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }


            }
        }

        private void btnHuyBo_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void DonViTinh_Load(object sender, EventArgs e)
        {
        }


    }
}